

<table class="layui-table layui-form category_list">
    <colgroup>
        <col width="20">
        <col width="50">
        <col>
        <col width="90">
        <col width="90">
        <col width="90">
        <col width="150">
        <col width="250">
    </colgroup>



    <thead>
    <tr>
        <th class="layui-table-cell">
            <input type="checkbox" lay-skin="primary"
                   lay-filter="check_all_checkbox"
                   id="check_all_checkbox">
        </th>
        <th>
            <div class="els2">图标</div>
        </th>
        <th>
            <div class="els2">名称</div>
        </th>
        <th>
            <div class="els2">排序</div>
        </th>
        <th>
            <div class="els2">数据</div>
        </th>
        <th>
            <div class="els2">发布状态</div>
        </th>
        <th>
            <div class="els2">备注</div>
        </th>
        <th>
            <div class="els2">操作</div>
        </th>
    </tr>
    </thead>




    <tbody>
    <?php foreach ($category as $k => $v) { ?>

        <tr id="item<? echo $v['nid'] ?>"
            class="category_01 <?if(!in_array($v['nid'],$category_search_result_id_list)){echo "passive";}?>">

            <td id="<? echo $v['nid'] ?>" class="layui-table-cell">
                <input type="checkbox" name="nid[]" lay-skin="primary"
                       lay-filter="item_checkbox"
                       value="<? echo $v['nid'] ?>">
            </td>

            <td>
                <div class="icon" id="icon<? echo $v['nid'] ?>">
                    <? if ($v['icon']) {
                        $icon = "/uploadfiles/" . $v['icon'];
                    } else {
                        $icon = "/controller/common/asset/image/no_image.jpg";
                    } ?>

                    <img src="<? echo $icon ?>"
                         style="object-fit: contain; width: 100%;height: 100%"
                         onclick="layer_show_by_id('<? echo 'icon' . $v['nid'] ?>')">
                </div>
            </td>

            <td>
                <div class="els2">
                    <?if ($v['level2']) {?><span class='ec_toggle'>+</span>
                    <?}else{?><span class='ec_toggle_disable'> </span><?}?>

                    <span class="category_name_01"><? echo $v['name'] ?></span>
                </div>
            </td>
            <td>
                <div class="els2"><? echo $v['listorder'] ?></div>
            </td>
            <td>
                <div class="els2">000</div>
            </td>
            <td>
                <input type="checkbox" <? if ($v['publish_status'] == "发布") {
                    echo "checked";
                } ?>
                       lay-skin="switch" value="<? echo $v['nid'] ?>"
                       lay-filter="publish_status"
                       lay-text="是|否">
            </td>

            <td>
                <div class="els1" id="remark"
                     onmouseover="show_remark('<? echo $v['remark'] ?>','remark')"
                     onmouseout="closed_remark('remark')">
                    <? echo $v['remark'] ?>
                </div>
            </td>


            <td>
                <span onclick="window_eject('category_edit_window.php?nid=<? echo $v['nid'] ?>')"
                      class="layui-btn  layui-btn-sm">
                      <i class="fa fa-edit" aria-hidden="true" /></i> 修改
                </span>
                <span onclick="window_eject('category_info_window.php?nid=<? echo $v['nid'] ?>')"
                      class="layui-btn  layui-btn-sm">
                                <i class="fa fa-file-text-o" aria-hidden="true" /></i> 信息
                </span>
                <span onclick="deleter(<?php echo $v['nid'] ?>,'category_delete_process')"
                      class="layui-btn layui-btn-sm layui-btn-primary">
                                <i class="fa fa-trash-o" aria-hidden="true" /></i> 删除
                </span>
            </td>


        </tr>


        <!--二级目录开始循环-->

        <?
        if (is_array($v['level2']))
        {

        foreach ($v['level2'] as $val2) {


            ?>
            <tr class="children_of_<? echo $v['nid'] ?>  category_02
                <?if(!in_array($val2['nid'],$category_search_result_id_list)){echo "passive";}?>"
                id="item<? echo $val2['nid'] ?>">

                <td id="<? echo $val2['nid'] ?>" class="layui-table-cell">
                    <input type="checkbox" name="nid[]" lay-skin="primary"
                           lay-filter="item_checkbox"
                           value="<? echo $val2['nid'] ?>">
                </td>
                <td>
                    <div class="icon" id="icon<? echo $val2['nid'] ?>">
                        <? if ($val2['icon']) {
                            $icon = "/uploadfiles/" . $val2['icon'];
                        } else {
                            $icon = "/controller/common/asset/image/no_image.jpg";
                        } ?>

                        <img src="<? echo $icon ?>"
                             style="object-fit: contain; width: 100%;height: 100%"
                             onclick="layer_show_by_id('<? echo 'icon' . $val2['nid']
                             ?>')">
                    </div>
                    </div>
                </td>
                <td>
                    <?if ($val2['level3']) {?><span class='ec_toggle'>+</span>
                    <?}else{?><span class='ec_toggle_disable'> </span><?}?>

                    <span class="category_name_01"><? echo $v['name'] ?></span> > <span class="category_name_02"><? echo $val2['name'] ?></span>
                </td>
                <td><? echo $val2['listorder'] ?></td>
                <td> 999 </td>


                <td>
                    <input type="checkbox" <? if ($val2['publish_status'] == "发布") {
                        echo "checked";
                    } ?>
                           lay-skin="switch" value="<? echo $val2['nid'] ?>"
                           lay-filter="publish_status"
                           lay-text="是|否">
                </td>

                <td>
                    <div class="els1" id="remark"
                         onmouseover="show_remark('<? echo $val2['remark'] ?>','remark')"
                         onmouseout="closed_remark('remark')">
                        <? echo $val2['remark'] ?>
                    </div>
                </td>


                <td>

                            <span onclick="window_eject('category_edit_window.php?nid=<? echo $val2['nid'] ?>')"
                                  class="layui-btn  layui-btn-sm">
                                  <i class="fa fa-bar-chart-o" aria-hidden="true"></i>修改
                            </span>

                    <span onclick="window_eject('category_info_window.php?nid=<? echo $val2['nid'] ?>')"
                          class="layui-btn  layui-btn-sm">
                                <i class="fa fa-bar-chart-o" aria-hidden="true"></i>信息
                            </span>

                    <span onclick="deleter(<?php echo $val2['nid'] ?>,'category_delete_process')"
                          class="layui-btn layui-btn-sm layui-btn-primary">
                                <i class="layui-icon">&#xe640;</i>删除
                            </span>
                </td>


            </tr>

            <!--3级目录开始循环-->
            <?
            if (is_array($val2['level3']))
            {
                foreach ($val2['level3'] as $val3)
            {

            ?>
                <tr id="item<? echo $val3['nid'] ?>"
                    class="children_of_<? echo $val2['nid'] ?> category_03
                    <?if(!in_array($val3['nid'],$category_search_result_id_list)){echo "passive";}?>">

                    <td id="<? echo $val3['nid'] ?>" class="layui-table-cell">
                        <input type="checkbox" name="nid[]" lay-skin="primary" lay-filter="item_checkbox"
                               value="<? echo $val3['nid'] ?>">
                    </td>
                    <td>
                        <div class="icon" id="icon<? echo $val3['nid'] ?>">
                            <? if ($val3['icon']) {
                                $icon = "/uploadfiles/" . $val3['icon'];
                            } else {
                                $icon = "/controller/common/asset/image/no_image.jpg";
                            } ?>

                            <img src="<? echo $icon ?>"
                                 style="object-fit: contain; width: 100%;height: 100%"
                                 onclick="layer_show_by_id('<? echo 'icon' . $val3['nid']
                                 ?>')">
                        </div>
                        </div>
                    </td>
                    <td>
                        <?if ($val3['level4']) {?><span class='ec_toggle'>+</span>
                        <?}else{?><span class='ec_toggle_disable'> </span><?}?>

                        <span class="category_name_01"><? echo $v['name'] ?></span> >
                        <span class="category_name_02"><? echo $val2['name'] ?></span> >
                        <span class="category_name_03"><? echo $val3['name'] ?></span>
                    </td>
                    <td><? echo $val3['listorder'] ?></td>
                    <td>000</td>


                    <td>
                        <input type="checkbox" <? if ($val3['publish_status'] == "发布") {
                            echo "checked";
                        } ?>
                               lay-skin="switch" value="<? echo $val3['nid'] ?>"
                               lay-filter="publish_status"
                               lay-text="是|否">
                    </td>

                    <td>
                        <div class="els1" id="remark"
                             onmouseover="show_remark('<? echo $val2['remark'] ?>','remark')"
                             onmouseout="closed_remark('remark')">
                            <? echo $val3['remark'] ?>
                        </div>
                    </td>


                    <td>

                            <span onclick="window_eject('category_edit_window.php?nid=<? echo $val3['nid'] ?>')"
                                  class="layui-btn  layui-btn-sm">
                                  <i class="fa fa-bar-chart-o" aria-hidden="true"></i>修改
                            </span>

                        <span onclick="window_eject('category_info_window.php?nid=<? echo $val3['nid'] ?>')"
                              class="layui-btn  layui-btn-sm">
                                <i class="fa fa-bar-chart-o" aria-hidden="true"></i>信息
                            </span>

                        <span onclick="deleter(<?php echo $val3['nid'] ?>,'category_delete_process')"
                              class="layui-btn layui-btn-sm layui-btn-primary">
                                <i class="layui-icon">&#xe640;</i>删除
                            </span>
                    </td>


                </tr>

                <!--3级目录开始循环-->
                <!--3级目录开始循环-->
                <!--3级目录开始循环-->
            <? } ?>




            <? } ?>
            <? } ?>
        <? ;} ?>


        <?php ;} ?>

    <?php //无数据时显示
    if (count($category) == 0) {
        echo "<tr class='no_data'><td colspan='15'>暂无数据</td></tr>";
    }
    ?>

    </tbody>
</table>




<script>




// 获取URL参数值的函数
function getRequestParam(name) {
    const urlParams = new URLSearchParams(window.location.search);
    console.log(urlParams.get(name));
    return urlParams.get(name);
}





function category_collapse_all()
    {
       //样式category_02,category_03的元素，都隐藏
        // 隐藏所有样式为category_02,category_03的元素
        $('.category_02').hide();
        $('.category_03').hide();
        $('span.ec_toggle').text('+');



    }
    function category_expand_all()
    {
        // 隐藏所有样式为category_02,category_03的元素
        $('.category_02').show();
        $('.category_03').show();
        //所有的$span.text('+');
        $('span.ec_toggle').text('-');
    }


    // 修复后的分类列表展开/收起控制脚本
    layui.use(['jquery'], function() {
        var $ = layui.jquery;

        //js中如果$_REQUEST['search_status'] 是"on"则执行category_expand_all();
// 检查URL参数中search_status是否为"on"，如果是则展开所有分类
        if (getRequestParam('search_status') === 'on')
        {
            category_expand_all();
        }
        else
        {
            // 初始化所有子级分类为隐藏状态
            $(document).ready(function() {
                // 隐藏所有二级和三级分类
                $('[class^="children_of_"]').hide();
            });
        }






        // 点击一级分类的 +/- 控制二三级分类同时显示/隐藏
        $('tbody').on('click', 'tr:not([class^="children_of_"]) td:nth-child(3) > div > span', function() {
            var $span = $(this);
            var $parentRow = $span.closest('tr');
            var parentId = $parentRow.attr('id').replace('item', '');

            // 查找该一级分类下的所有二级分类
            var $children = $('.children_of_' + parentId);

            if ($children.is(':visible')) {
                // 如果当前是展开状态，则收起
                $children.hide();
                // 同时收起三级分类并重置所有二级分类的toggle状态
                $children.each(function() {
                    var level2Id = $(this).attr('id').replace('item', '');
                    $('.children_of_' + level2Id).hide();
                    // 重置该二级分类下的toggle按钮状态
                    $(this).find('td:nth-child(3) > span.ec_toggle').text('+');
                });
                $span.text('+');
            } else {
                // 如果当前是收起状态，则展开（但不自动展开三级分类）
                $children.show();
                // 只重置二级分类的toggle状态，不自动展开三级分类
                $children.each(function() {
                    var hasChildren = $(this).find('td:nth-child(3) > span.ec_toggle').length > 0;
                    if (hasChildren) {
                        // 如果二级分类有子分类，保持+状态（需要用户手动点击展开）
                        $(this).find('td:nth-child(3) > span.ec_toggle').text('+');
                        // 确保三级分类是隐藏的
                        var level2Id = $(this).attr('id').replace('item', '');
                        $('.children_of_' + level2Id).hide();
                    }
                });
                $span.text('-');
            }
        });

        // 点击二级分类的 +/- 控制三级分类显示/隐藏
        $('tbody').on('click', 'tr[class^="children_of_"] td:nth-child(3) > span', function() {
            var $span = $(this);
            var $parentRow = $span.closest('tr');
            var parentId = $parentRow.attr('id').replace('item', '');

            // 查找该二级分类下的所有三级分类
            var $children = $('.children_of_' + parentId);

            if ($children.is(':visible')) {
                // 如果当前是展开状态，则收起
                $children.hide();
                $span.text('+');
            } else {
                // 如果当前是收起状态，则展开
                $children.show();
                $span.text('-');
            }
        });
    });


</script>



